/**
 * 
 */
package net.mikoo.seals.biz.service.impl;

import org.apache.commons.lang.StringUtils;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import net.mikoo.seals.biz.mapper.PersonMapper;
import net.mikoo.seals.biz.service.PersonServices;
import net.mikoo.seals.common.enums.GenderEnum;
import net.mikoo.seals.common.enums.MobileStatusEnum;
import net.mikoo.seals.common.exception.DataNotFoundException;
import net.mikoo.seals.common.exception.ParametersException;
import net.mikoo.seals.model.Person;

/**
 * @author huangxm
 *
 */
@Service
public class PersonServicesImpl implements PersonServices {

	Logger logger = Logger.getLogger(PersonServicesImpl.class);

	@Autowired
	private PersonMapper personMapper;

	@Override
	public Person loadByPrimaryKey(String userLoginId) {
		
		Person person = personMapper.selectByPrimaryKey(userLoginId);

		return person;
	}

	@Override
	public Person modifyByPrimaryKey(Person person) {
		
		personMapper.updateByPrimaryKey(person);

		return person;
	}

	@Override
	public void deleteByPartyId(Long partyId) {
		if (partyId == null) {
			throw new ParametersException("partyId is null!");
		}
		personMapper.deleteByPartyId(partyId);

	}

	@Override
	public void modifyName(Long partyId, String name) {
		
		if(partyId == null) {
			throw new ParametersException("获取参数失败");
		}
		
		if(StringUtils.isBlank(name)) {
			throw new ParametersException("用户名不能为空");
		}
		
		Person person = personMapper.selectByPartyId(partyId);
		if(person == null) {
			throw new DataNotFoundException("用户信息不存在");
		}
		person.setName(name);
		personMapper.updateByPrimaryKey(person);
	}

	@Override
	public void modifyMobile(Long partyId, String mobile) {
		if(partyId == null) {
			throw new ParametersException("获取参数失败");
		}
		
		if(StringUtils.isBlank(mobile)) {
			throw new ParametersException("手机号不能为空");
		}
		
		Person person = personMapper.selectByPartyId(partyId);
		if(person == null) {
			throw new DataNotFoundException("用户信息不存在");
		}
		person.setMobile(mobile);
		personMapper.updateByPrimaryKey(person);
		
	}

	@Override
	public void modifyEmail(Long partyId, String email) {
		if(partyId == null) {
			throw new ParametersException("获取参数失败");
		}
		
		if(StringUtils.isBlank(email)) {
			throw new ParametersException("email不能为空");
		}
		
		Person person = personMapper.selectByPartyId(partyId);
		if(person == null) {
			throw new DataNotFoundException("用户信息不存在");
		}
		person.setEmail(email);
		personMapper.updateByPrimaryKey(person);
		
	}

	@Override
	public void modifyNickname(Long partyId, String nickname) {
		if(partyId == null) {
			throw new ParametersException("获取参数失败");
		}
		
		if(StringUtils.isBlank(nickname)) {
			throw new ParametersException("昵称不能为空");
		}
		
		Person person = personMapper.selectByPartyId(partyId);
		if(person == null) {
			throw new DataNotFoundException("用户信息不存在");
		}
		person.setNickname(nickname);
		personMapper.updateByPrimaryKey(person);
		
	}

	@Override
	public void modifyGender(Long partyId, String gender) {
		if(partyId == null) {
			throw new ParametersException("获取参数失败");
		}
		
		GenderEnum genderEnum = GenderEnum.getByCode(gender);
		if(genderEnum == null) {
			throw new ParametersException("性别不能为空");
		}
		
		Person person = personMapper.selectByPartyId(partyId);
		if(person == null) {
			throw new DataNotFoundException("用户信息不存在");
		}
		person.setGender(gender);
		personMapper.updateByPrimaryKey(person);
		
	}

	@Override
	public void modifyPortrait(Long partyId, String imgUrl) {
		if(partyId == null) {
			throw new ParametersException("获取参数失败");
		}
		
		if(StringUtils.isBlank(imgUrl)) {
			throw new ParametersException("头像不能为空");
		}
		
		Person person = personMapper.selectByPartyId(partyId);
		if(person == null) {
			throw new DataNotFoundException("用户信息不存在");
		}
		person.setPortrait(imgUrl);;
		personMapper.updateByPrimaryKey(person);
	}

	@Override
	public Person getByPartyId(Long partyId) {
		
		if(partyId == null) {
			throw new ParametersException("partyId IS NULL");
		}
		Person person = personMapper.selectByPartyId(partyId);
		return person;
	}

	@Override
	public void modifyMobileStatus(Long partyId, String mobileStatus) {
		if(partyId == null) {
			throw new ParametersException("partyId is null");
		}
		
		MobileStatusEnum mobileStatusEnum= MobileStatusEnum.getByCode(mobileStatus);
		if(mobileStatusEnum == null) {
			throw new ParametersException("性别不能为空");
		}
		
		Person person = personMapper.selectByPartyId(partyId);
		if(person == null) {
			throw new DataNotFoundException("用户信息不存在");
		}
		person.setMobileStatus(mobileStatus);
		personMapper.updateByPrimaryKey(person);
		
	}
}
